'use strict'

let successState = 0 // 表示成功
let fialState = 1 // 表示失败
// 1.0 7牛云存储域名
// let domain = 'http://ofv795nmp.bkt.clouddn.com/'
let domain = 'http://demo.dtcms.net'

exports.getlunbo = (req, res) => {
  let resObj = {
    status: successState, message: [{
		id: 1,
      url: 'http://www.itcast.cn/subject/phoneweb/index.html',
      // img: 'http://www.liulongbin.top:3005/images/lunbo1-min.jpg'
		  img: 'http://destiny001.gitee.io/image/ban1.jpg'
    }, {
		id: 2,
      url: 'http://www.itcast.cn/subject/phoneweb/index.html',
      // img: 'http://www.liulongbin.top:3005/images/lunbo2-min.jpg'
		img: 'http://destiny001.gitee.io/image/ban2.jpg'
    }, {
		id: 3,
      url: 'http://www.itcast.cn/subject/phoneweb/index.html',
      // img: 'http://www.liulongbin.top:3005/images/lunbo3-min.jpg'
		img: 'http://destiny001.gitee.io/image/ban3.jpg'
    },
    {
      id: 4,
      url: 'http://www.itcast.cn/subject/phoneweb/index.html',
        // img: 'http://www.liulongbin.top:3005/images/lunbo3-min.jpg'
      img: 'http://destiny001.gitee.io/image/ban4.jpg'
    }
  ]
  }

  res.end(JSON.stringify(resObj))

}
exports.xx = (req, res) => {res.end("xxxxx")}

// 1.0 获取图片新闻资讯列表
exports.getnewslist = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  // 3.0 利用orm发送sql语句查询出来分页数据即可
  /*
  id : 资讯主键
  ,title : 资讯标题
  ,add_time ：资讯创建的事件
  ,zhaiyao：摘要
  ,click：点击量
  ,img_url:图片地址，前缀是7牛云存储域名
   */
  let sql = " SELECT id,title,add_time,left(zhaiyao,25) as zhaiyao,click,concat('" + domain + "',img_url) as img_url FROM dt_article where img_url > '' and channel_id = 6 limit 0,10 "
  console.log('获取图文资讯sql语句：============>', sql)
  req.db.driver.execQuery(sql, (err, datas) => {
    // 4.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 5.0 获取数据成功
    resObj.message = datas
    res.end(JSON.stringify(resObj))
  })
}

// 2.0 根据资讯id获取资讯详细内容
exports.getnew = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  // 1.0 获取参数值
  let newid = req.params.newid

  // 2.0 执行查询操作
  let sql = 'select id,title,click,add_time,content from dt_article  where id=' + newid
  console.log('获取资讯明细sql===>', sql)
  req.db.driver.execQuery(sql, (err, data) => {
    // 3.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 4.0 获取数据成功
    resObj.message = data
    res.end(JSON.stringify(resObj))
  })
}

// 3.0 商品
exports.getgoods = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }
  let pageindex = req.query.pageindex
  if (!pageindex) {
    pageindex = 1;
  }
  let pagesize = 10
  let skipcount = (pageindex - 1) * pagesize

  // 3.0 利用orm发送sql语句查询出来分页数据即可
  /*
  id : 资讯主键
  ,title : 资讯标题
  ,add_time ：资讯创建的事件
  ,zhaiyao：摘要
  ,click：点击量
  ,img_url:图片地址，前缀是7牛云存储域名
   */
  let sql = `SELECT a.id,a.title,a.add_time,left(a.zhaiyao,25) as zhaiyao,a.click,concat('${domain}',a.img_url) as img_url,b.sell_price,b.market_price,b.stock_quantity FROM dt_article as a,dt_article_attribute_value b where a.id = b.article_id and a.channel_id = 7 limit ${skipcount},${pagesize} `
  console.log('获取图文资讯sql语句：============>', sql)
  req.db.driver.execQuery(sql, (err, datas) => {
    // 4.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 5.0 获取数据成功
    resObj.message = datas
    res.end(JSON.stringify(resObj))
  })
}





// 3.2 商品
exports.getgoods2 = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }
  let pageindex = req.query.pageindex
  let category_id = req.query.category_id
  if (!pageindex) {
    pageindex = 1;
  }
  let pagesize = 6
  let skipcount = (pageindex - 1) * pagesize

  // 3.0 利用orm发送sql语句查询出来分页数据即可
  /*
  id : 资讯主键
  ,title : 资讯标题
  ,add_time ：资讯创建的事件
  ,zhaiyao：摘要
  ,click：点击量
  ,img_url:图片地址，前缀是7牛云存储域名
   */
  let sql = `SELECT a.id,a.title,a.add_time,left(a.zhaiyao,25) as zhaiyao,a.click,concat('${domain}',a.img_url) as img_url,b.sell_price,b.market_price,b.stock_quantity FROM dt_article as a,dt_article_attribute_value b where a.id = b.article_id and a.channel_id = 7 AND  a.category_id= ${category_id}    limit ${skipcount},${pagesize} `
  console.log('获取图文资讯sql语句：============>', sql)
  req.db.driver.execQuery(sql, (err, datas) => {
    // 4.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 5.0 获取数据成功
    resObj.message = datas
    res.end(JSON.stringify(resObj))
  })
}





/*
3.0.1 获取商品详情页面数据
-- 获取商品详情页标题，图文介绍信息等
SELECT * FROM dt_article da WHERE da.channel_id = 7 AND da.title LIKE '%新科%';
-- 获取商品详情页中的滚动图片
select * FROM dt_article_albums daa WHERE daa.article_id = 101;

-- 获取商品参数区域信息
SELECT daav.goods_no,daav.stock_quantity FROM dt_article_attribute_value daav  WHERE daav.article_id =101;

-- 商品品论
select * from dt_article_comment dac WHERE dac.article_id=101
 */

// 商品图文描述
exports.getgooddesc = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  let id = req.params.id;
  let sql = ` SELECT title,content FROM dt_article da WHERE da.id = ${id} `
  console.log('获取商品图文描述sql语句：============>', sql)
  req.db.driver.execQuery(sql, (err, datas) => {
    // 4.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 5.0 获取数据成功
    resObj.message = datas
    res.end(JSON.stringify(resObj))
  })
}

// 获取商品标题，价格，参数区数据
// getgoodsinfo
exports.getgoodsinfo = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  let id = req.params.id;
  let sql = ` SELECT da.id,da.title,da.add_time,daa.goods_no,daa.stock_quantity,daa.market_price,daa.sell_price FROM dt_article da , dt_article_attribute_value daa 
				WHERE  da.id = daa.article_id and da.id = ${id} `
  console.log('获取商品获取商品标题，价格，参数区数据sql语句：============>', sql)
  req.db.driver.execQuery(sql, (err, datas) => {
    // 4.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 5.0 获取数据成功
    resObj.message = datas
    res.end(JSON.stringify(resObj))
  })
}

//获取购物车列表数据
exports.getshopcarlist = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  // 1.0 获取参数值
  let ids = req.params.ids

  // 2.0 执行查询操作
  let sql = `
  			  SELECT count(distinct tb1.id) as cou, tb1.* FROM (
				SELECT  da.id,da.title,daa.sell_price,alb.thumb_path
				  FROM dt_article da 
				  LEFT JOIN dt_article_attribute_value daa ON (da.id = daa.article_id)
				  LEFT JOIN dt_article_albums alb ON (da.id = alb.article_id)
				WHERE  da.id IN(${ids}) ) AS tb1 GROUP BY tb1.id
  `

  console.log('获取购物车列表sql===>', sql)
  req.db.driver.execQuery(sql, (err, data) => {
    // 3.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }
		
		
		// http://ofv795nmp.bkt.clouddn.com/
		// http://vue.studyit.io
	  data.forEach(item => item.thumb_path = 'http://demo.dtcms.net' + item.thumb_path)

    // 4.0 获取数据成功
    resObj.message = data
    res.end(JSON.stringify(resObj))
  })
}


// 4.0 图片分享
exports.getimages = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  let cateid = req.params.cateid - 0

  let sql = ' select id,title,img_url,zhaiyao from dt_article where channel_id = 9 and category_id=' + cateid

  if (cateid <= 0) {
    sql = ' select * from dt_article where channel_id = 9 '
  }

  // 3.0 利用orm发送sql语句查询出来分页数据即可
  /*

   */

  console.log('获取图片分享sql语句：============>', sql)
  req.db.driver.execQuery(sql, (err, datas) => {
    // 4.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

	  datas.forEach(item => {
		// http://ofv795nmp.bkt.clouddn.com/
		// http://vue.studyit.io
		item.img_url = 'http://demo.dtcms.net' + item.img_url
	  })

    // 5.0 获取数据成功
    resObj.message = datas
    res.end(JSON.stringify(resObj))
  })
}


// 4.0.1 根据id获取图片详细内容
exports.getimage = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  // 1.0 获取参数值
  let newid = req.params.imgid

  // 2.0 执行查询操作
  let sql = `select thumb_path as src  from dt_article_albums where article_id =${newid}`

  console.log('获取图片分享明细中缩略图sql===>', sql)
  req.db.driver.execQuery(sql, (err, data) => {
    // 3.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }
	
	data.forEach(item => {
		// http://ofv795nmp.bkt.clouddn.com/
		// http://vue.studyit.io
		item.src = 'http://demo.dtcms.net' + item.src
	})

    // 4.0 获取数据成功
    resObj.message = data
    res.end(JSON.stringify(resObj))
  })
}


// 4.0.1 根据id获取图片详细内容
exports.getimageInfo = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  // 1.0 获取参数值
  let newid = req.params.imgid

  // 2.0 执行查询操作
  let sql = `select id,title,click,add_time,content from dt_article where id=${newid}`

  console.log('获取图片分享明细sql===>', sql)
  req.db.driver.execQuery(sql, (err, data) => {
    // 3.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 4.0 获取数据成功
    resObj.message = data
    res.end(JSON.stringify(resObj))
  })
}

// 5.0 获取图片分享分类
exports.getimgcategory = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  // 3.0 利用orm发送sql语句查询出来分页数据即可
  /*

   */
  let sql = ' select title,id from dtcmsdb4.dt_article_category where channel_id = 9 '
  console.log('获取图片分享分类sql语句：============>', sql)
  req.db.driver.execQuery(sql, (err, datas) => {
    // 4.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 5.0 获取数据成功
    resObj.message = datas
    res.end(JSON.stringify(resObj))
  })
}

//6.0 获取评论信息
exports.getcomments = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  // 1.0 获取参数值
  let artid = req.params.artid
  let pageindex = req.query.pageindex
  let pagesize = 10;
  let skipCount = (pageindex - 1) * pagesize

  // 2.0 执行查询操作
  let sql = `select user_name,add_time,content from dt_article_comment where article_id = ${artid} order by add_time desc limit ${skipCount},${pagesize}`

  console.log('获取评论sql===>', sql)
  req.db.driver.execQuery(sql, (err, data) => {
    // 3.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 4.0 获取数据成功
    resObj.message = data
    res.end(JSON.stringify(resObj))
  })
}


//7.0 提交评论数据
exports.postcomment = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  // 1.0 获取参数值
  let artid = req.params.artid
  let commentObj = req.body;


  // 2.0 执行查询操作
  let sql = `insert into  dt_article_comment(channel_id,article_id,parent_id,user_id,user_name,user_ip,
                                  content,is_lock,add_time,is_reply,reply_content,reply_time)
                    values (7,${artid},0,0,'匿名用户','127.0.0.1','${commentObj.content}',0,NOW(),0,'',NOW())`

  console.log('post提交评论sql===>', sql)
  req.db.driver.execQuery(sql, (err, data) => {
    // 3.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 4.0 获取数据成功
    resObj.message = '评论提交成功'
    res.end(JSON.stringify(resObj))
  })

}


// 获取品牌列表
exports.getprodlist = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  var sql = 'select * from dt_brands order by id';
  req.db.driver.execQuery(sql, (err, data) => {
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
    } else {
      resObj.message = data
      res.end(JSON.stringify(resObj))
    }
  });
}


// 根据品牌Id删除品牌列表
exports.delproduct = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  var id = req.params.id;

  var sql = 'delete from dt_brands where id=' + id;
  req.db.driver.execQuery(sql, (err, data) => {
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
    } else {
      resObj.message = '删除品牌数据OK'
      res.end(JSON.stringify(resObj))
    }
  });
}


// 添加新的品牌信息
exports.addproduct = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  var info = req.body;

  // var sql = 'insert into dt_brands (name, ctime) values ("' + info.name + '", "' + new Date() + '")';
  var sql = 'insert into dt_brands (name, ctime) values (?, ?)';
  req.db.driver.execQuery(sql, [info.name, new Date()], (err, data) => {
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
    } else {
      resObj.message = '添加成功'
      res.end(JSON.stringify(resObj))
    }
  });
}

// 获取评论列表
exports.getcmtlist = (req, res) => {
	// 代表返回的数据结构
	let resObj = { status: successState, message: '' }
	var sql = "select * from dt_comments order by id desc limit 0, 10"
	req.db.driver.execQuery(sql, (err, data) => {
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
    } else {
      resObj.message = data
      res.end(JSON.stringify(resObj))
    }
  });
}

// 发表新评论
exports.postcmt = (req, res) => {
	// 代表返回的数据结构
	let resObj = { status: successState, message: '' }
	// 提交过来的数据
	var info = req.body;
	var sql = "insert into dt_comments (name, content) values(?,?)"
	req.db.driver.execQuery(sql, [info.name,info.content], (err, data) => {
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
    } else {
      resObj.message = '发表评论成功！'
      res.end(JSON.stringify(resObj))
    }
  });
}


  // 2.0 执行查询操作
// 引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
// 数据库连接
mongoose.connect('mongodb://140.143.244.192/gaole', { useNewUrlParser: true})
	// 连接成功
	.then(() => console.log('数据库连接成功'))
	// 连接失败
	.catch(err => console.log(err, '数据库连接失败'));
// 创建集合规则
const userSchema = new mongoose.Schema({
  id: Number,
  mobile: Number,
  nickname: String,
  portrait: String,
  password: String,
  email:String,
});

// 使用规则创建集合
const User = mongoose.model('User', userSchema);
console.log("User model 已经创建了")


//mix电商项目的登录。
exports.login_mix = (req, res) => {
  // 代表返回的数据结构
   // 1.0 获取参数值
  let username = req.body.username;
// 定义好了 Schema，接下就是生成 Model。model 是由 schema 生成的模型，可以对数据库的操作。
// 注意：mongoose.model 里面可以传入两个参数也可以传入三个参数，如果传入 2 个参数的话:这个模型会和与模型名称相同的、名称为复数的集合建立连接：如通过下面方法创建模型，那么这个模型将会操作 users 这个集合。
// var User=mongoose.model('User', UserSchema);//users
// 如果传入 3 个参数的话:模型默认操作与第三个参数同名的集合
// var User=mongoose.model('User', UserSchema,'user');

// User.create({	id: 1,mobile: 18888888888,nickname: 'Leo yo',portrait: 'http://img.61ef.cn/news/201409/28/2014092805595807.jpg',password: '123'})
	  // .then(result => {
	  	// console.log(result)
	  // })
	  
// 查询用户集合中的所有文档
// User.find().then(result => console.log(result));
// 通过_id字段查找文档
// User.find({_id: '61ced8a28f330653c9a91605'}).then(result => console.log(result))
// var res_mongo;
// findOne方法返回一条文档 默认返回当前集合中的第一条文档
User.findOne({mobile: username}).then(
  result => {console.log("result--->",result);
            //  res_mongo=result;
             if (result == null || result == undefined) 
             { 
               res.end(JSON.stringify({"mobile":"0","status":0}))
               return
             }

             let resObj = new Object;
             resObj=result.toObject();// nodejs 不能直接的添加元素，需要 .toObject()
             resObj.status=0;
             console.log("add status result -->",resObj)
             res.end(JSON.stringify(resObj))
            }
  )
// 查询用户集合中年龄字段大于20并且小于40的文档
// User.find({age: {$gt: 20, $lt: 40}}).then(result => console.log(result))
// 查询用户集合中hobbies字段值包含足球的文档
// User.find({hobbies: {$in: ['足球']}}).then(result => console.log(result))
// 选择要查询的字段
// User.find().select('name email -_id').then(result => console.log(result))
// 根据年龄字段进行升序排列
// User.find().sort('age').then(result => console.log(result))
// 根据年龄字段进行降序排列
// User.find().sort('-age').then(result => console.log(result))
// 查询文档跳过前两条结果 限制显示3条结果
// User.find().skip(2).limit(3).then(result => console.log(result))
}



//order表
const orderSchema = new mongoose.Schema({
  mobile:Number,
year: Number,
create_time: String,
all_total: Number,
coupon: Number,
order_detail: Array,
});
// 使用规则创建集合  ORDER 表
const Order = mongoose.model('order', orderSchema);

exports.find_order = (req, res) => {
   // 1.0 获取参数值
  //  let username = req.body.username;
  //  let mobile = req.params.mobile;
  //  let tabindex = req.params.tabindex;  占位符的形式rest风格
   let mobile = req.query.mobile 
   let tabindex = req.query.tabindex 
console.log("req params:",mobile+"---"+tabindex);
   Order.find({mobile: mobile,year:tabindex}).then(
    result => {console.log(result);
    console.log("-------------");
    // console.log(result[0].order_detail);
    if (result == null || result == undefined) 
    { 
      res.end(JSON.stringify({"mobile":"0","status":0}))
      return
    }

    let resObj = new Object;
    resObj={list:result,status:0};
    // result.toObject();// nodejs 不能直接的添加元素，需要 .toObject()
    // resObj.status=0;
    console.log("add status result -->",resObj)
    res.end(JSON.stringify(resObj))

  }
);
}


const cartSchema = new mongoose.Schema({
  mobile:Number,
  id: Number,
  image: String,
  attr_val: String,
  stock: Number,
  title: String,
  price: Number,
  number: Number,
  });
  const Cart = mongoose.model('cart', cartSchema);
exports.getmixcart = (req, res) => {
  let mobile = req.query.mobile ;
  console.log("req params:",mobile+"---");

  Cart.find({mobile: mobile}).then(
    result => {console.log(result);
    console.log("-------------");
    // console.log(result[0].order_detail);
    if (result == null || result == undefined) 
    { 
      res.end(JSON.stringify({"mobile":"0","status":0}))
      return
    }

    let resObj = new Object;
    resObj={list:result,status:0};
    // result.toObject();// nodejs 不能直接的添加元素，需要 .toObject()
    // resObj.status=0;
    console.log("add status result -->",resObj)
    res.end(JSON.stringify(resObj))

  }
);
}


//添加到购物车的时候，先查看有没有把这个商品，以及型号添加进来，如果添加了，就提示已经存在了，不用重复的添加
exports.getmixcart_hasornot = (req, res) => {
  let mobile = req.query.mobile ;
  let title = req.query.title ;
  let attr_val = req.query.attr_val ;
  console.log("req params:",mobile+"---");

  Cart.find({mobile: mobile,title:title,attr_val:attr_val}).then(
    result => {console.log(result);
    console.log("-------------");
    // console.log(result[0].order_detail);
    if (result == null || result == undefined || result.length<1) 
    { 
      res.end(JSON.stringify({"mobile":"0","status":0}))
      return
    }

    let resObj = new Object;
    resObj={"mobile":"1",status:0};
    // result.toObject();// nodejs 不能直接的添加元素，需要 .toObject()
    // resObj.status=0;
    console.log("add status result -->",resObj)
    res.end(JSON.stringify(resObj))

  }
);
}

//添加到购物车
exports.addmixcart = (req, res) => {
  let cartitem = req.body
  const arr=[cartitem]
  console.log("req cartitem--:",  cartitem);

  Cart.insertMany(arr).then(
    result => {console.log(result);
    console.log("-------------");
    // console.log(result[0].order_detail);
    if (result == null || result == undefined || result.length<1) 
    { 
      res.end(JSON.stringify({"mobile":"0","status":0}))
      return
    }

    let resObj = new Object;
    resObj={"list":resObj,status:0};
    // result.toObject();// nodejs 不能直接的添加元素，需要 .toObject()
    // resObj.status=0;
    console.log("add status result -->",resObj)
    res.end(JSON.stringify(resObj))

  }
);
}





var ObjectId = require('mongodb').ObjectID;



exports.delmixcart = (req, res) => {
  let _id = req.params.id ;
  console.log("req params:",_id+"---");

  // Cart.remove({_id: _id}).then(
    Cart.remove({"_id":ObjectId(_id)})
    .then(
    result => {console.log(result);
    console.log("-------------");
  
    if (result == null || result == undefined) 
    { 
      res.end(JSON.stringify({"mobile":"0","status":0}))
      return
    }

    let resObj = new Object;
    resObj={list:result,status:0};
    // result.toObject();// nodejs 不能直接的添加元素，需要 .toObject()
    // resObj.status=0;
    console.log("add status result -->",resObj)
    res.end(JSON.stringify(resObj))

  }
);
}


const carouselListSchema = new mongoose.Schema({
  src:String,
  background: String,
  });
  const CarouselList = mongoose.model('carouselList', carouselListSchema);
  
  exports.carouselList = (req, res) => {
       CarouselList.find().then(
      result => {console.log(result);
      console.log("-------------");
      // console.log(result[0].order_detail);
      if (result == null || result == undefined) 
      { 
        res.end(JSON.stringify({"mobile":"0","status":0}))
        return
      }
      let resObj = new Object;
      resObj={list:result,status:0};
      // result.toObject();// nodejs 不能直接的添加元素，需要 .toObject()
      // resObj.status=0;
      console.log("add status result -->",resObj)
      res.end(JSON.stringify(resObj))
  
    }
  );
  }


  

  const goodsListSchema = new mongoose.Schema({
    image:String,
    image2: String,
    image3: String,
    title: String,
    price: Number,
    sales: Number,
    });
    const GoodsList = mongoose.model('goodsList', goodsListSchema);
  
  exports.goodsList = (req, res) => {
    GoodsList.find().then(
   result => {console.log(result);
   console.log("-------------");
   // console.log(result[0].order_detail);
   if (result == null || result == undefined) 
   { 
     res.end(JSON.stringify({"mobile":"0","status":0}))
     return
   }
   let resObj = new Object;
   resObj={list:result,status:0};
   // result.toObject();// nodejs 不能直接的添加元素，需要 .toObject()
   // resObj.status=0;
   console.log("add status result -->",resObj)
   res.end(JSON.stringify(resObj))

 }
);
}

// 商品的详情页面
const detailPageSchema = new mongoose.Schema({
  cate:String,
  attr_val:String,
  flag:String,
  title:String,
  hit: Number,
  saleamount: Number,
  price_main: Number,
  imgList: Array,
  specList: Array,
  specChildList:Array,
  });
  const DetailPage = mongoose.model('detailPage', detailPageSchema);


exports.detailPage = (req, res) => {
  // 1.0 获取参数值
 //  let username = req.body.username;
 //  let mobile = req.params.mobile;
 //  let tabindex = req.params.tabindex;  占位符的形式rest风格
  let title = req.query.title 
 console.log("req params:",title);
DetailPage.findOne({title: title}).then(
   result => {console.log(result);
   console.log("-------------");
   // console.log(result[0].order_detail);
   if (result == null || result == undefined) 
   { 
     res.end(JSON.stringify({"mobile":"0","status":0}))
     return
   }

   let resObj = new Object;
   resObj={list:result,status:0};
   // result.toObject();// nodejs 不能直接的添加元素，需要 .toObject()
   // resObj.status=0;
   console.log("add status result -->",resObj)
   res.end(JSON.stringify(resObj))
 }
);
}





exports.detailPage_index = (req, res) => {
  // 1.0 获取参数值
 //  let username = req.body.username;
 //  let mobile = req.params.mobile;
 //  let tabindex = req.params.tabindex;  占位符的形式rest风格
  let flag = req.query.flag 
 console.log("req params:",flag);
DetailPage.find({flag: flag}).then(
   result => {console.log(result);
   console.log("-------------");
   if (result == null || result == undefined) 
   { 
     res.end(JSON.stringify({"mobile":"0","status":0}))
     return
   }

   let resObj = new Object;
   resObj={list:result,status:0};
   // result.toObject();// nodejs 不能直接的添加元素，需要 .toObject()
   // resObj.status=0;
   console.log("add status result -->",resObj)
   res.end(JSON.stringify(resObj))
 }
);
}





exports.detailPage_cate = (req, res) => {
  let cate = req.query.cate 
 console.log("req params:",cate);
DetailPage.find({cate: cate}).then(
   result => {console.log(result);
   console.log("-------------");
   if (result == null || result == undefined)
   { 
     res.end(JSON.stringify({"mobile":"0","status":0}))
     return
   }
   let resObj = new Object;
   resObj={list:result,status:0};
   console.log("add status result -->",resObj)
   res.end(JSON.stringify(resObj))
 }
);
}